// 老师列表
import {teacherGet, teacherInfo, teacherModify, teacherDelete, deleteTeacher} from "../../api/teacherList"

const state = {

    teacherRecord: [],//老师列表
    teacherInfo: {
        teacherId: '',
        account: '', //工号
        name: '', //老师姓名
        password: '', //老师密码
        college: '',//学院
        e_mail: '',//邮箱
        phone: ''//电话
    },//老师详细信息
}

const mutations = { // mutations改变state中的数据

    SET_TEACHER_RECORD: (state, teacherRecord) => { //修改老师信息
        let data=[]
        for(let i of teacherRecord){
            data.push({
                key:i.teacherId,
                account:i.account,
                name:i.name,
                password:i.password,
                college:i.college
            })
        }
        state.teacherRecord = data
    },

    //修改老师详细信息
    SET_TEACH_RECORD: (state, teachRecord) => {
        state.teacherInfo.teacherId=teachRecord.simpleTeacherInfo.teacherId
        state.teacherInfo.account=teachRecord.simpleTeacherInfo.account
        state.teacherInfo.name=teachRecord.simpleTeacherInfo.name
        state.teacherInfo.password=teachRecord.simpleTeacherInfo.password
        state.teacherInfo.college=teachRecord.simpleTeacherInfo.college
        state.teacherInfo.e_mail=teachRecord.email
        state.teacherInfo.phone=teachRecord.phone
    },

    // 删除老师列表记录
    DELETE_TEACHER: (state, account) => {
        let data=state.teacherRecord
        data=data.filter(n=>{
            return n.key!==account
        })
        state.teacherRecord=data
    },

    //批量删除老师列表数据
    MASS_DELETE_TEACHER: (state, accounts) => {
        let data=state.teacherRecord;
        accounts.map(item=>{
            data=data.filter(n=>{
                return n.key!==item
            })
        })
        state.teacherRecord=data
    },

    MODIFY_TEACHER_INFO:(state,editTeacher)=>{
        let data=state.teacherRecord
        for(let i of data){
            //console.log(i)
            if(i.key===editTeacher.teacherId){
                i.account=editTeacher.account
                i.name=editTeacher.name
                i.password=editTeacher.password
                i.phone=editTeacher.phone
                i.e_mail=editTeacher.e_mail

                break
            }
        }
        state.teacherRecord=data

    },

    NULL: () => {}
}

const actions = { // 发送请求

    //获取老师列表数据
    getTeacherList({commit},data){
       //token如何添加
       // const token = data
        return new Promise((resolve,reject) => {
            teacherGet(data).then((data) => {
                if(data.code === 1){
                    //console.log(token)
                    commit('SET_TEACHER_RECORD',data.data);
                    resolve(data);
                }else{
                    reject(data.msg);
                }
            }).catch(error=>{
                reject(error)
            })
        })
    },

    teacherInfo({commit},data) {//获取老师详细信息
        return new Promise((resolve,reject) => {
            teacherInfo(data).then((res) => {
                //console.log(res)
                if(res.code === 1){
                    commit('SET_TEACH_RECORD',res.data);
                    resolve(res);
                }else{
                    reject(res.msg);
                }
            }).catch(error=>{
                reject(error)
            })
        })
    },

    //删除单条老师信息
    teacherDelete({commit},data) {
        return new Promise((resolve, reject) => {
            teacherDelete(data).then(res => {
                    if (res.code === 1) {
                        resolve(res)
                        commit("NULL")
                    }else {
                        reject(res.msg)
                    }
                }
            ).catch(error => {
                reject(error)
            })
        })
    },

    // 修改老师信息
    teacherModify({commit}, data){
        return new Promise((resolve, reject) => {
            teacherModify(data).then(res => {
                    if (res.code === 1) {
                        resolve(res)
                        commit("NULL")
                    }else{
                        reject(res.msg)
                    }
                }
            ).catch(error => {
                reject(error)
            })
        })
    },

    //批量删除老师
    massDeleteTeacher({commit},data) {
        return new Promise((resolve, reject) => {
            deleteTeacher(data).then(res => {
                    if (res.code === 1) {
                        commit("NULL")
                        resolve(res)
                    }else{
                        reject(res.msg)
                    }
                }).catch(error => {
                reject(error)
            })
        })
    },
}


export default {
    namespaced: true,
    state,
    mutations,
    actions
}
